/**
 * controller_money.js
 * 现金控制器
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-03-05
 */

//资金分配列表
app.controller('MoneyDeductList', function($scope, $location, $http, common){
	$scope.list 	= {};
	$scope.loading 	= false;
	$scope.nPage 	= 1;
	$scope.getType 	= 'all';
	$scope.pageHtml 	= '';
	$scope.keyword 	= '';
	$scope.dateStart 	= '';
	$scope.dateEnd 	= '';
	$scope.total 	= {};
	$scope.breadcrumb 	= [];

	//初始化JS
	$scope.init_js 	= function(){
		$('.input-date').datetimepicker({
	        language:  'zh-CN',
	        format: 'yyyy-mm-dd',
	        weekStart: 1,
	        todayBtn:  1,
	        autoclose: 1,
	        todayHighlight: 1,
	        startView: 2,
	        forceParse: 0,
	        showMeridian: 1,
	        minView	: 2
	    });

	    //标签页钩子设置
        $('a[data-toggle="tab"]').on('show.bs.tab', function(e){
            var type    = $(this).data('target');
            switch(type){
                case '#list-all': 
                	$scope.getType 	= 'all';
                break;
                case '#list-expand' : 
                	$scope.getType 	= 'expand';
                break;
                default: return false; break;
            }
            $scope.load_data(1);
        });

	    //回车键监听
	    common.listenEnterKey(function(){
	        $scope.load_data(1);
	    });
	}

	//加载数据
	$scope.load_data 	= function(page){
		var url 	= config.prefix_url+'/Money/getDeductList?'+
			'&p='+page+
			'&date_start='+$scope.dateStart+
			'&date_end='+$scope.dateEnd+
			'&keyword='+$scope.keyword+
			'&get_type='+$scope.getType;

		$http.get(url).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			$scope.nPage 	= page;
			$scope.list 	= info.data.list;
			$scope.pageHtml 	= info.data.page_html;
			$scope.total 	= info.data.total;
			$scope.loading 	= false;
			common.startPage($scope.load_data);

			$scope.breadcrumb 	= [
				{'name'	: '全部记录', 'item' : false}
			];
		});
		$scope.loading 	= true;
		$scope.list 	= [];
	}

	//详情
	$scope.detail 	= function(item){
		$scope.nItemInfo 	= item;
		$('.modal.bargain-detail').modal('show');
	}

	//导出excel
	$scope.out_excel 	= function(){
		var url 	= config.prefix_url + '/Money/outExcelDeductList?'+
			'&keyword='+$scope.keyword+
			'&date_start='+$scope.dateStart+
			'&date_end='+$scope.dateEnd;

		$http.get(url).success(function(info){
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			var count 	= info.data.count;
			var length 	= 2000;
			var start 	= 0;
			while(start < count){
				var outUrl 	= url + '&start='+start+'&length='+length;
				window.open(outUrl, '_blank');
				start 	+= length;
			}
		})
	}

	//获取下级用户分红
	$scope.get_child_list 	= function(item, page){
		if(item === false || item.role_id == 101){
			$scope.load_data(1);
			return false;
		}
		if(item.role_id != 101){
			common.doAlert('该角色不是幸福合伙人!');
			return false;
		}
		var page 	= page ? page : 1;
		var url 	= config.prefix_url + '/Money/getChildDeductList?p='+page+'&user_id='+item.user_id;
		$http.get(url).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			$scope.breadcrumb.push({'name': item.nickname, 'item' : item});
			$scope.nPage 	= page;
			$scope.list 	= info.data.list;
			$scope.pageHtml 	= info.data.page_html;
			$scope.total 	= info.data.total;

			common.startPage(function(page){
				$scope.get_child_list(item, page);
			})
		})

		$scope.loading 	= true;
		$scope.list 	= [];
		$scope.breadcrumb 	= [
			{'name'	: '全部记录', 'item' : false}
		];
	}

	$scope.load_data(1);
	$scope.init_js();
})